<template>
	<view class="content">
		<view class="top">
			<u-toast ref="uToast" />
			<view class="btn-list" @click="addItme">
				<image :src="addimg" mode="widthFix"></image>
			</view>
			<view class="bg">
				<image src="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-uni1eaa20b/d4e94f10-823c-11ea-8bd0-2998ac5bbf7e.png" mode="widthFix"></image>
			</view>
			<view class="header">
				<view>
					<text>今天要做</text>
					<text class="number">{{ toDayList.length }}</text>
					<text>件事情~</text>
				</view>
			</view>
		</view>
		<scroll-view scroll-y="true">
			<u-empty v-if="!toDayList.length" text="点击+号新建一个吧" mode="list"></u-empty>
			<view v-else class="list" v-for="(item, index) in toDayList" :key="item._id">
				<u-gap height="30" bg-color="none"></u-gap>
				<view class="item">
					<view class="leftcolor" :style="{
							backgroundColor: color16(index)
						}"></view>
					<view class="center" @click="watchMessage(item._id)" @longpress="detileMessage(item._id)">
						<view class="title">{{ item.title }}</view>
						<u-gap height="10" bg-color="none"></u-gap>
						<view class="time">{{ item.addTime }}</view>
					</view>
					<view v-if="item.issend" class="image">
						<u-icon @click="showtips" name="bell" :color="item.sendsuccess == 2 ? 'green' : item.sendsuccess == 3 ? 'red' : 'inherit'"></u-icon>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {
		uToast
	} from '@/uview/components/u-toast/u-toast.vue';
	import {
		mapGetters
	} from 'vuex';
	export default {

		data() {
			return {
				addimg: require('@/static/add.png'),
				toDayList: [],
				isfirst: true, // 是否为第一次加载
				show: false // 小闹钟的提醒
			};
		},
		computed: {
			...mapGetters(['openid'])
		},
		onLoad() {
			let _this = this;
		},
		async onShow() {
			if (!this.openid) {
				await this.getUserInfoByOpenid();
				this.getListInfo();

			} else {
				this.getListInfo();

			}
		},
		methods: {

			/** 添加新的记录
			 */
			addItme() {
				if (!this.openid) {
					return this.$refs.uToast.show({
						title: '您还没有登录，点击我的去登录一下吧~',
						type: 'primary'
					});
				}
				uni.navigateTo({
					url: '../addNewInfo/addNewInfo'
				});
			},
			showtips() {
				let arr = ['小铃铛表示小主需要提醒哦~', '绿色是已经提醒过了呢~', '红色是提醒出错了QAQ'];
				let index = Math.floor(Math.random() * 3);
				this.$refs.uToast.show({
					title: arr[index],
					type: 'primary'
				});
			},
			/**
			 * 删除消息
			 */
			async detileMessage(id) {
				uni.showActionSheet({
					itemList: ['A', 'B', 'C'],
					success: function(res) {
						console.log('选中了第' + (res.tapIndex + 1) + '个按钮');
					},
					fail: function(res) {
						console.log(res.errMsg);
					}
				});
			},
			async getListInfo() {
				let _this = this;
				if (this.isfirst) {
					uni.showLoading({
						title: '努力加载中...'
					});
				}
				let time = new Date()
				uniCloud
					.callFunction({
						name: 'getListInfo',
						data: {
							type: 'today',
							openid: _this.openid,
							day: time.getDate(),
							month: time.getMonth() + 1,
							year: time.getFullYear()
						}
					})
					.then(res => {
						if (res.success) {
							uni.hideLoading();
							_this.isfirst = false;
							_this.toDayList = res.result.data;
						}
					})
					.catch(error => {
						uni.hideLoading();
						_this.isfirst = false;
						console.log(error);
					});
			},
			async getuserinfo(res) {
				let openid = await this.getOpenid();
				let userInfo = res.target.userInfo;
				userInfo.openid = openid;
				uni.showLoading({
					title: '处理中...'
				});
				uniCloud
					.callFunction({
						name: 'addUserInfo',
						data: userInfo
					})
					.then(res => {
						uni.hideLoading();
						uni.showModal({
							content: `成功添加一条数据，文档id为：${res.result.id}`,
							showCancel: false
						});
						console.log(res);
					})
					.catch(err => {
						uni.hideLoading();
						uni.showModal({
							content: `添加数据失败，错误信息为：${err.message}`,
							showCancel: false
						});
						console.error(err);
					});
			},
			getUserInfoByOpenid() {
				return new Promise(async resolve => {
					let openid = await this.getOpenid();
					let result = await uniCloud.callFunction({
						name: 'addUserInfo',
						data: {
							type: 'get',
							openid: openid
						}
					});
					if (result.success) {
						this.$store.dispatch('setUserinfo', result.result).then(res => {
							resolve();
						});
					}
				});
			},

			watchMessage(id) {
				uni.navigateTo({
					url: `../messageDetile/messageDetile?id=${id}`
				});
			}
		}
	};
</script>

<style lang="scss">
	$add-width: 100rpx;

	.content {
		padding-bottom: 10px;
		display: flex;
		height: 100vh;
		flex-direction: column;
	}

	scroll-view {
		flex: 1;
		height: 1rpx;
	}

	.bg {
		width: 100vw;
		height: auto;

		image {
			width: 100vw;
		}
	}

	.header {
		padding-top: $page-herder-pading + 100;
		position: fixed;
		top: 0;
		width: 100vw;
		z-index: 100;
		text-align: center;
		color: #fff;
		text-align: center;
		font-size: 40rpx;
		font-weight: 300;

		.number {
			font-size: 50rpx;
			color: $u-type-warning-dark;
			font-weight: bold;
		}
	}

	.btn-list {
		position: fixed;
		z-index: 999;
		bottom: 100rpx;
		right: 50rpx;
		height: $add-width;
		line-height: $add-width;
		width: $add-width;
		border-radius: 50%;
		text-align: center;
		background-color: $u-type-warning;
		display: flex;
		justify-items: center;
		align-items: center;

		image {
			margin: 0 auto;
			display: block;
			width: $add-width / 2;
			height: $add-width / 2;
		}
	}

	.list {
		padding: $page-padding;

		.item {
			border-radius: 10rpx;
			overflow: hidden;
			display: flex;
			background-color: #fff;

			.leftcolor {
				display: block;
				width: 10rpx;
				height: inherit;
				background: red;
			}

			.center {
				padding: $page-padding;
				margin: 20rpx 0;
				flex: 1;

				.title {
					font-size: 32rpx;
					color: #333;
				}

				.time {
					color: u-type-info-disabled;
					font-size: 24rpx;
				}
			}

			.image {
				display: flex;
				align-items: center;
				padding: 0 30rpx;
				border-radius: 10rpx;
			}
		}
	}
</style>
